<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.tabDati.php');

$idSetDati = reqVarInt('sd');
$sqlSel = "SELECT d.* "
		." FROM rgl_def_dato d "
		." WHERE d.cod_dato = '".reqVarAS('cd')."' AND d.id_set_dati = ".$idSetDati;
$objDB->query($sqlSel,'selRec');
if (!$acRec = $objDB->fetch('selRec')) {
	backOnErr("var. cd. '".reqVar('cd')."' mancante o errato");
}
$_REQUEST = array_merge($acRec,$_REQUEST);

$aCamForm = array();
require("_inc/const.campiDato.php");

if (reqVar('azione') == 'salva') {
	$msgPag = salvaDato($aCamForm);
} 

$sCmdR = "";
$sCmdR .= "<a class='cmd1' href=\"javascript:document.frm1.azione.value='salva';document.frm1.submit();\">salva</a>";
$sCmdR .=  cmdTorna('torna'); 

testaHtml('Gestione modelli',$msgPag,'modl');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         <? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo getHtmlForm('frm1','',array('enctype'=>true));
echo getHtmlHidden('azione','reload');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('sd');
echo getHtmlHidden('cd');
echo getHtmlHidden('cod_dato');
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="lab1" colspan='20' align='center'>Definizione Dato</td>
   </tr>
<?
// rende non modificabile il campo cod_dato
foreach($aCamForm as $rr => $aX) {
	foreach($aX as $cc => $aY) {
		if ($aY[0]=='cod_dato') {
			$aCamForm[$rr][$cc][4] = 'string';
		}
	}
}
echo getHtmlCampiForm($aCamForm);
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         <? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();

function salvaDato($aCamForm) {
	global $objDB;
	$msg = '';
	/*////////////////// cod dato non modificabile
	$_REQUEST['cod_dato'] = strtoupper(trim(reqVar('cod_dato')));
	if (reqVar('cod_dato')=='') {
		$msg .= _t("Codice obbligatorio")."<br>";
	}
	if (preg_match("/[^_A-Z0-9]/",reqVar('cod_dato'))) {
		$msg = _t("Codice '%s' puo' contenere solo lettere numeri e '_'",reqVar('cod_dato'))."<br>";
	}
	if (reqVar('cod_dato') != reqVar('cd')) {
		$sqlSel = "SELECT cod_dato FROM rgl_def_dato " .
				" WHERE cod_dato = '".reqVarAS('cod_dato')."' AND id_set_dati = ".reqVarInt('sd');
		$objDB->exec($sqlSel);
		$aRow = $objDB->fetch();
		if ($aRow) {
			$msg .= _t("Codice gia' esistente")."<br>";
		}
	}
	*////////////////////////////
	if (trim(reqVar('descr'))=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	
	//check script
	$script = reqVarTrim("script");
	//dbg_($script);
	$objParse = new rglParser;
	if(!$objParse->parse($script)) {
		return $objParse->getError();
	}
	$aVarAbil = getVarAbilCalcDato(reqVar('sd'),reqVar('cd')); 
	
	//dbg_("aVarAbil: ".print_r($aVarAbil,1));
	//dbg_("objParse->aVariables: ".print_r($objParse->aVariables,1));
	foreach($objParse->aVariables as $varName => $aPropVar) {
		if (strpos('TRS_',$varName[0])!== false) { // temporary or system variable
			continue;
		}
		if (!isset($aVarAbil[$varName])) {
			$msg .= _t("variabile '%s' non disponibile",$varName)."<br>";
			continue;
		}
		if ($aPropVar['modified'] and $aVarAbil[$varName] == 'non-modif') {
			$msg .= _t("variabile '%s' non modificabile",$varName)."<br>";
		}
	}

	$aFunzDisp = array();  // TODO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	foreach($objParse->aFunctions as $funzName => $aFunzVar) {
		if (!isset($aFunzDisp[$funzName])) {
			$msg .= _t("function '%s' not available",$funzName)."<br>";
			continue;
		}
		if ($aFunzVar['numPar'] !=  count($aFunzDisp[$funzName])) {
			$msg .= _t("function '%s' with %s parameters - required %s",
				$funzName,$aFunzVar['numPar'],count($aFunzDisp[$funzName]))."<br>";
		}
	}
		
	if($msg != "") {
		return $msg;
	}	
	if (reqVar('reformat')=='S') {
		$script = $objParse->getFormatCode();
	}
	/*//////////////////////////////
	$nomeFunz = 'rglScrDato'.reqVarAS('cod_dato');
	$pathFile = RGL_FS_CODGEN_DIR."/".$nomeFunz.".php";
	$phpCode = $objParse->getPHPCode($nomeFunz);
	file_put_contents($pathFile,$phpCode);
	//*/////////////////////////////////
	$sSql = "UPDATE rgl_def_dato SET cod_sez = '".reqVarAS('cod_sez')."'"
		.",descr = '".reqVarAS('descr')."'"
		.",cod_dato_padre = '".reqVarAS('cod_dato_padre')."'"
		.",tipo_valore = '".reqVarAS('tipo_valore')."'"
		.",flag_quad_calc = '".reqVarAS('flag_quad_calc')."'"
		.",script = '".addslashes($script)."'"
		.",situaz_progr = '".reqVarAS('situaz_progr')."'"
		.",segnala_variaz = ".reqVarInt('segnala_variaz')
		.",cod_fonte = '".reqVarAS('cod_fonte')."'"
		." WHERE cod_dato = '".reqvarAS('cd')."'";
	$objDB->exec($sSql);
	header("Location: ".urlTorna("Dato '".reqVar('cod_dato')."' modificato"));
	exit();
}
